# Merging Lists
# Lists are created using square brackets.
L1 = [1, 2, 5, 12]
L2 = [6, 7]
print(L1)
print(L2)
[1, 2, 5, 12] [6, 7]
# You can merge the two lists using the addition notation.
print(L1 + L2)
[1, 2, 5, 12, 6, 7]
# If you're using arrays made from the 'NumPy' module, merging, or concatenating
# two arrays is a little different.
import numpy as np
A1 = np.array(L1)
A2 = np.array(L2)
A3 = np.concatenate((A1, A2))
print(A3)
[ 1 2 5 12 6 7]
# You can also build matrices from arrays (assuming the arrays have the same length)
A1 = np.array([1, 2, 3, 4])
A2 = np.array([-5, -6, -7, -8])
M1 = np.matrix([A1, A2])
print(M1)
[[ 1 2 3 4] [-5 -6 -7 -8]]
# The matrix can then be transposed:
M2 = np.transpose(M1)
print(M2)
[[ 1 -5] [ 2 -6] [ 3 -7] [ 4 -8]]
# Here's a way that a list can be buily using a loop.
# First, define an empty list.
listA = []
# Next add to the list in successive iterations of a loop
for i in range(9):
listA = listA + [i**2.5]
# Now display the contents of the list.
listA
[0.0, 1.0, 5.656854249492381, 15.588457268119896, 32.0, 55.90169943749474, 88.18163074019441, 129.64181424216494, 181.01933598375618]